import uuid
import re

def generate_16_char_uuid():
    # 生成一个随机的 UUID4
    random_uuid = uuid.uuid4()
    # 将 UUID 转换为字符串，并截取前16位
    return str(random_uuid).replace('-', '')[:16]

# 将十六进制字符串转换为二进制字符串
def hex_to_bin(hex_str):
    bin_str = bin(int(hex_str, 16))[2:]  # 去掉 '0b' 前缀
    return bin_str

# 将二进制字符串转换为十六进制字符串
def bin_to_hex(bin_str):
    hex_str = hex(int(bin_str, 2))[2:]  # 去掉 '0x' 前缀
    return hex_str.zfill((len(bin_str) + 3) // 4)  # 填充到十六进制宽度


def read_coe_file(file_path):
    memory_initialization_radix = None
    memory_initialization_vector = None

    with open(file_path, 'r') as file:
        content = file.read()

    # 使用正则表达式提取 memory_initialization_radix
    radix_match = re.search(r'memory_initialization_radix\s*=\s*(\d+)\s*;', content, re.IGNORECASE)
    if radix_match:
        memory_initialization_radix = radix_match.group(1)

    # 使用正则表达式提取 memory_initialization_vector
    vector_match = re.search(r'memory_initialization_vector\s*=\s*([^;]+)\s*;', content, re.IGNORECASE)
    if vector_match:
        memory_initialization_vector = vector_match.group(1).strip()

    return memory_initialization_radix, memory_initialization_vector
